package com.tech.connect.zhaofuwu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.ksy.common.image.ImageLoader;
import com.ksy.common.image.ImagePickActivity;
import com.ksy.common.utils.ParamsUtil;
import com.ksy.oss.OssClient;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;
import com.tech.connect.activity.EditTextActivity;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.CurrentInfo;
import com.tech.connect.api.FuWuZhanHttp;
import com.tech.connect.model.CatChoose;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class CreateFuWuZhanActivity extends BaseActivity {

    private ImageView iv;
    private TextView tvName;
    private TextView tvCat;
    private TextView tvNote;
    private String imageUrl;
    private CatChoose mainCat;
    private CatChoose subCat;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_fuwuzhan);
        getHeadBar().setTitle("创建服务站").setRightText("完成", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = tvName.getText().toString().trim();
                String note = tvNote.getText().toString().trim();
                if (isEmpty(name)) {
                    showToast("请输入服务站名称");
                    return;
                }
                if (subCat == null) {
                    showToast("请选择服务站分类");
                    return;
                }
                if (isEmpty(note)) {
                    showToast("请输入服务站公告");
                    return;
                }
                final Map<String, Object> map = new HashMap<>();
//                map.put("areaCode", "");
                map.put("lat", CurrentInfo.getUserInfo().lat);
                map.put("lng", CurrentInfo.getUserInfo().lng);
                map.put("name", name);
                map.put("type", subCat.id);

                if (!isEmpty(imageUrl)) {
                    showDialog();
                    OssClient.getInstance().update(activity, ParamsUtil.getInstance().create(OssClient.FileType.Image, imageUrl), new OssClient.UpdateListener() {
                        @Override
                        public void onFinish(final boolean isSuccess, final String ossPath) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    if (isSuccess) {
                                        map.put("logo", ossPath);
                                        update(map);
                                    } else {
                                        showToast("图片上传失败");
                                    }
                                }
                            });

                        }
                    });
                } else {
                    update(map);
                }
            }
        });
        initView();
    }

    private void update(Map<String, Object> map) {
        FuWuZhanHttp.create(map, new BaseEntityOb<String>() {
            @Override
            public void onStart() {
                super.onStart();
                showDialog();
            }

            @Override
            public void onDataDeal(boolean isSuccess, String data, String msg) {
                hideDialog();
                showToast(msg);
                if (isSuccess) {
                    setResult(RESULT_OK);
                    destroyActivity();
                }
            }
        });
    }

    private void initView() {
        iv = findViewById(R.id.iv);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImagePickActivity.comeIn(activity, false, 6, 1001);
            }
        });
        tvName = findViewById(R.id.tvName);
        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, EditTextActivity.class);
                intent.putExtra("data", tvName.getText().toString().trim());
                jump2Activity(intent, 1);
            }
        });
        tvCat = findViewById(R.id.tvCat);
        tvCat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(ZhaoFuWuFuWuCatChooseActivity.class, 2);
            }
        });
        tvNote = findViewById(R.id.tvNote);
        tvNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, EditTextActivity.class);
                intent.putExtra("data", tvNote.getText().toString().trim());
                jump2Activity(intent, 3);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == Activity.RESULT_OK && intent != null) {
            if (requestCode == 1001) {
                ArrayList<String> paths = intent.getStringArrayListExtra(ImagePickActivity.Data_selectImages);
                if (paths != null) {
                    imageUrl = paths.get(0);
                }
                if (isEmpty(imageUrl)) {
                    ImageLoader.getInstance().load(activity, R.drawable.icon_image_adder, iv, new RequestOptions().centerCrop());
                } else {
                    ImageLoader.getInstance().load(activity, imageUrl, iv, new RequestOptions().centerCrop());
                }
            } else if (requestCode == 1) {
                String data = intent.getStringExtra("data");
                tvName.setText(data);
            } else if (requestCode == 2) {
                Serializable mainCatS = intent.getSerializableExtra("mainCat");
                Serializable subCatS = intent.getSerializableExtra("subCat");
                if (mainCatS != null && subCatS != null && mainCatS instanceof CatChoose && subCatS instanceof CatChoose) {
                    mainCat = (CatChoose) mainCatS;
                    subCat = (CatChoose) subCatS;
                    tvCat.setText(mainCat.name + "/" + subCat.name);
                }
            } else if (requestCode == 3) {
                String data = intent.getStringExtra("data");
                tvNote.setText(data);
            }
        }
    }
}
